﻿namespace Knownet.Utils
{
  using System;
  using System.Text;
  using System.IO;

  /// <summary>
  /// 日志。
  /// </summary>
  public class Report
  {
    #region Statics
    public static readonly Report Instance = CreateInstance();
    public static Report CreateInstance()
    {
      return new Report();
    }
    public static Report of(TextWriter writer)
    {
      return new Report(writer);
    }
    #endregion
    #region Fields
    protected TextWriter writer;
    #endregion
    #region Properties
    public virtual TextWriter Out
    {
      get
      {
        return this.writer;
      }
    }
    #endregion
    #region Constructors
    public Report() : this(null) { }
    public Report(TextWriter writer)
    {
      this.writer = (writer != null) ? writer : Console.Out;
    }
    #endregion
    #region Methods
    public virtual void ReportMessage(string text)
    {
      this.Out.WriteLine(text);
    }
    public virtual void ReportError(string text)
    {
      this.Error(text, Location.Null);
    }
    public virtual void Error(string text,Location l)
    {
      if (l != null && ! l.IsNull)
      {
        text = (text != null) ? text : "Errors";

        this.Out.WriteLine(text + " at " + l.ToString());
      }
      else
      {
        text = (text != null) ? text : "NO DETAILS";

        this.Out.WriteLine("Errors found :" + text);
      }
    }
    #endregion
  }
}
